package avicit.bdp.dms.perm.rest;

import avicit.bdp.dms.perm.service.CategoryPermService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 模型分类权限Rest接口
 */
@RestController
@Api(tags = "业务领域分类权限管理Rest接口")
@RequestMapping("/api/bdp/dms/perm/category")
public class CategoryPermRest {
    @Autowired
    private CategoryPermService categoryPermService;

    /**
     * 新增数据权限
     * @param roleId 角色ID
     * @param nodeId 模型分类节点ID，多个节点使用英文逗号隔开
     * @param actionList 权限，0：查看;1：编辑;2：数据查看
     * 注意：
     *     1、支持分开授予查看、编辑、数据查看权限；
     *     2、VUE会将本次设置权限、已设置权限一并传入；
     * @return
     */
    @ApiOperation(value = "新增业务领域分类权限")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色id", name = "roleId", dataType = "String", required = true),
            @ApiImplicitParam(value = "节点id", name = "nodeId", dataType = "String", required = true),
            @ApiImplicitParam(value = "拥有权限", name = "actionList", dataType = "List", required = true)
    })
    @PostMapping("/addCategoryPerm")
    public ResponseMsg addCategoryPerm(@RequestParam String roleId,
                                       @RequestParam String nodeId,
                                       @RequestParam List<Integer> actionList) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.categoryPermService.addCategoryPerm(roleId, nodeId, actionList);

        return responseMsg;
    }

    /**
     * 移除数据权限
     * 注意：
     *     1、新增授权时，如果为编辑或数据查看，多增加一条查看记录（related=1），删除时要一并删除
     * @param roleId 角色ID
     * @param nodeId 模型分类节点ID，多个节点使用英文逗号隔开
     * @param actionList 待移除权限
     * @return
     */
    @ApiOperation(value = "移除数据权限")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色id", name = "roleId", dataType = "String", required = true),
            @ApiImplicitParam(value = "节点id", name = "nodeId", dataType = "String", required = true),
            @ApiImplicitParam(value = "拥有权限", name = "actionList", dataType = "List", required = true)
    })
    @DeleteMapping("/removeCategoryPerm")
    public ResponseMsg removeCategoryPerm(@RequestParam String roleId,
                                          @RequestParam String nodeId,
                                          @RequestParam List<Integer> actionList) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.categoryPermService.removeCategoryPerm(roleId, nodeId, actionList);

        return responseMsg;
    }

    /**
     * 根据节点ID删除权限(业务模块删除模型分类节点时同步删除权限)
     * @param nodeId
     * @return
     */
    @ApiOperation(value = "根据节点ID删除权限")
    @ApiImplicitParam(value = "节点id", name = "nodeId", dataType = "String", required = true)
    @DeleteMapping("/deleteCategoryPermByNodeId")
    public ResponseMsg<Integer> deletePermByNodeId(@RequestParam String nodeId) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        this.categoryPermService.deletePermByNodeId(nodeId);

        return responseMsg;
    }
}
